package user;

import vehicle.Bus;
import vehicle.Car;
import vehicle.Truck;
import vehicle.Vehicle;
import java.util.List;
import java.util.Scanner;

public class Administrator extends User {
    private Scanner scanner;

    public Administrator(String username, String password) {
        super(username, password);
        this.scanner = new Scanner(System.in);
    }

    @Override
    public void operate(List<Vehicle> vehicleList) {
        boolean loop = true;
        while (loop) {
            System.out.println("管理员操作菜单：");
            System.out.println("1. 添加车辆");
            System.out.println("2. 删除车辆");
            System.out.println("3. 修改车辆信息");
            System.out.println("4. 查看营业额");
            System.out.println("5. 退出");
            try {
                int choice = scanner.nextInt();
                scanner.nextLine();
                switch (choice) {
                    case 1:
                        addVehicle(vehicleList);
                        break;
                    case 2:
                        deleteVehicle(vehicleList);
                        break;
                    case 3:
                        modifyVehicle(vehicleList);
                        break;
                    case 4:
                        viewTurnover(vehicleList);
                        break;
                    case 5:
                        loop = false;
                        break;
                    default:
                        System.out.println("无效选择，请重新输入。");
                }
            } catch (Exception e) {
                System.out.println("输入错误，请重新输入。");
                scanner.nextLine();
            }
        }
    }

    private void addVehicle(List<Vehicle> vehicleList) {
        System.out.println("请选择要添加的车辆类型：1. 汽车 2. 卡车 3. 巴士");
        try {
            int vehicleType = scanner.nextInt();
            scanner.nextLine();
            System.out.println("请输入车辆车牌：");
            String licensePlate = scanner.nextLine();
            System.out.println("请输入车辆品牌：");
            String brand = scanner.nextLine();
            if (vehicleType == 1) {
                vehicleList.add(new vehicle.Car(licensePlate, brand));
            } else if (vehicleType == 2) {
                vehicleList.add(new vehicle.Truck(licensePlate, brand));
            } else if (vehicleType == 3) {
                vehicleList.add(new vehicle.Bus(licensePlate, brand));
            } else {
                System.out.println("无效的车辆类型选择。");
            }
            System.out.println("成功添加车辆：" + licensePlate);
        } catch (Exception e) {
            System.out.println("输入错误，请重新输入。");
            scanner.nextLine();
        }
    }

    private void deleteVehicle(List<Vehicle> vehicleList) {
        System.out.println("请输入要删除的车辆车牌：");
        String deletePlate = scanner.nextLine();
        boolean found = false;
        for (int i = 0; i < vehicleList.size(); i++) {
            Vehicle vehicle = vehicleList.get(i);
            if (vehicle.getLicensePlate().equals(deletePlate)) {
                vehicleList.remove(i);
                found = true;
                System.out.println("成功删除车辆：" + deletePlate);
                break;
            }
        }
        if (!found) {
            System.out.println("未找到指定车辆。");
        }
    }

    private void modifyVehicle(List<Vehicle> vehicleList) {
        System.out.println("请输入要修改的车辆车牌：");
        String modifyPlate = scanner.nextLine();
        boolean found = false;
        for (Vehicle vehicle : vehicleList) {
            if (vehicle.getLicensePlate().equals(modifyPlate)) {
                found = true;
                System.out.println("车辆类型：" + vehicle.getClass().getSimpleName() + "，车牌：" + vehicle.getLicensePlate() + "，品牌：" + vehicle.getBrand());
                System.out.println("请输入新的车辆品牌：");
                String newBrand = scanner.nextLine();
                vehicle.brand = newBrand;
                System.out.println("成功修改车辆信息。");
                break;
            }
        }
        if (!found) {
            System.out.println("未找到指定车辆。");
        }
    }

    private void viewTurnover(List<Vehicle> vehicleList) {
        System.out.println("正在计算营业额...");
        int turnover = 0;
        for (Vehicle vehicle : vehicleList) {
            if (vehicle.isRented()) {
                if (vehicle instanceof Car) {
                    turnover += 50 * vehicle.getRentDays();
                } else if (vehicle instanceof Truck) {
                    if (vehicle.getRentDays() <= 5) {
                        turnover += 100 * vehicle.getRentDays();
                    } else {
                        turnover += 100 * 5 + 95 * (vehicle.getRentDays() - 5);
                    }
                } else if (vehicle instanceof Bus) {
                    if (vehicle.getRentDays() <= 5) {
                        turnover += 80 * vehicle.getRentDays();
                    } else {
                        turnover += 80 * 5 + (int) (80 * 0.8 * (vehicle.getRentDays() - 5));
                    }
                }
            }
        }
        try {
            Thread.sleep(3000);
            System.out.println("营业额为：" + turnover);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}